package com.edu.study.entities;

import com.edu.base.common.utils.ObjectUtils;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.List;


@Data
@Accessors(chain = true)
@NoArgsConstructor
public class RPage<T> {

  private long total;

  /** 列表数据 */
  private List<T> rows;

  /** 消息状态码 */
  private int code;

  /** 消息内容 */
  private String msg;

  // 当前数据页码
  private Integer pageNum;

  // 当前每页显示条数
  private Integer pageSize;

  // 总页数
  private Integer totalPage;

  // 是否还有下一页
  private Boolean hasNextPage;

  public static RPage of(PageInfo<?> pageInfo, String msg, List<?> rows) {
    return buildPage(pageInfo, msg, rows);
  }

  public static RPage of(PageInfo<?> pageInfo, String msg) {
    return buildPage(pageInfo, msg, null);
  }

  private static RPage buildPage(PageInfo<?> pageInfo, String msg, List<?> rows) {
    RPage rPage = new RPage();
    rPage.setTotal(pageInfo.getTotal());

    rPage.setRows(ObjectUtils.isEmpty(rows) ? pageInfo.getList() : rows);

    rPage.setCode(200);
    rPage.setMsg(msg);
    rPage.setPageNum(pageInfo.getPageNum());
    rPage.setPageSize(pageInfo.getPageSize());
    rPage.setTotalPage(pageInfo.getPages());
    rPage.setHasNextPage(pageInfo.isHasNextPage());
    return rPage;
  }
}
